* GENERAL INFO
	* Project: Senegal Games
	* Created by: Jessica Gottlieb
	* Date created: June 2014
	* Updated: 
	* Updated by: 
* DO FILE INFO
	* This .do file analyzes results of game and survey data from Senegal 2014 field work.

* DATA FILES CALLED BY THIS .DO FILE
	* Participant_merged.dta \\ Participant survey data
	* Leader.dta \\ Leader survey data
*********************************************************************************************************************
set more off

global types Diola Toucouleur Mouride 

* Merge leader and hh data with game data
use "Participant_merged", clear
generate solid_hh=0
replace solid_hh=1 if house_mat=="2" | house_mat=="4" | house_mat=="5" | village==11
replace land=. if land==-88 | land==-9 | land==-1 
foreach var of varlist age_cohort-trustintpol{
ren `var' `var'_hh
}
ren edlevel edlevel_hh
sort village
merge village using "Leader"
label define type1 1 "Diola" 2 "Toucouleur" 3 "Mouride", modify
label value type1 type1
replace type1=2 if type=="Toucouleur"
replace type1=3 if type=="Mouride"
label var vote1 "Vote for leader-preferred outcome"

* Descriptive stats
replace namepolint_hh=0 if namepolint_hh==-9
tabu namepolint_hh 
mean namepolint_hh, over(type1)
gen namepolint1=1-namepolint_hh
bysort village: egen pnamepolint=mean(namepolint1)
label var pnamepolint "Proportion correctly naming broker"

* Construct main independent variables

**Competitive
generate competitive=0
replace competitive=1 if selection_polint==3 | other_15=="Selection par des jeunes du village aussi" | other_15=="Selection par les jeunes" | other_15=="choisi par les jeunes du village" |other_15=="d�signation par la population de Santack" | other_15=="selection des jeunes du village" |other_15=="selection par le village" |other_15=="selection par les sages et les jeunes" | other_15=="Nomination" | other_15=="nomination" 
replace competitive=1 if village==1
replace candidates_p="" if candidates_p=="m"
destring candidates_p, replace
label variable competitive "Competitive leader selection"
label variable candidates_p "Number of potential candidates for leader"

**Autonomy
replace land=3 if village==25
generate nonag=0
replace nonag=1 if resources_fin=="12" | resources=="2" | resources=="2a" | resources=="5a"
replace nonag=1 if other_18=="Affaires politiques" | other_18=="Commerce" | other_18=="Heritage paternel" | other_18=="marabout" | other_18=="op곡teur ꤯nomique"
generate sourceswealth=0
replace sourceswealth=1 if resources=="2"  | resources=="a" | resources=="1"
replace sourceswealth=2 if resources=="2a" | resources=="5a" | resources=="12" | resources=="1a"
label var sourceswealth "Number of sources of wealth"

egen meanland=mean(land_hh), by(village)
generate rland=land/meanland
label var rland "Ratio of leader to participant landholding (in ha)"
label var land "Leader landholding (in ha)"
label variable land_hh "Participant land (in ha)"
label var abroad "Spends over 1 month out of village"

do genindex.do
genindex competitive candidates_p, nv(comp)
genindex rland abroad sourceswealth, nv(auto)
center autoA
center compA
label variable autoA "Autonomy index"
label variable compA "Competitive index"
foreach num of numlist 1/3{
sum autoA if type1==`num', meanonly
local a`num' = r(mean)
sum compA if type1==`num', meanonly
local c`num' = r(mean)
}
twoway (scatter compA autoA if type=="Mouride", sort msymbol(circle) mcolor(gs1) text(`c3' `a3' "M", place(c))) (scatter compA autoA if type=="Toucouleur", sort msymbol(triangle) mcolor(gs10) text(`c2' `a2' "T", place(c))) (scatter compA autoA if type=="Diola", sort msymbol(square) mcolor(gs6) text(`c1' `a1' "D", place(c)) ), ///
legend(order(1 "Mouride" 2 "Toucouleur" 3 "Diola" 4 "M=Mouride (mean)" 5 "T=Toucouleur (mean)" 6 "D=Diola (mean)") col(3)) ///
xlabel(-1.5(.5)1.5) ylabel(-1.5(.5)1.5) yline(0) xline(0) graphregion(color(white))
graph export "Graphs/scatter.pdf", replace

**Reciprocity
replace noncash_hh=. if noncash_hh==-9
egen transfer=mean(noncash_hh), by(village)
label variable transfer "Share participants receiving transfers from leader"

* Graphing group-level differences
tempfile main
save "`main'"
global leaderdv vote1 candidates_p competitive abroad sourceswealth land land_hh transfer

label var vote1 "Percent vote for leader-preferred outcome"
 foreach v in $leaderdv {
 local l`v' : variable label `v'
        if `"`l`v''"' == "" {
 		local l`v' "`v'"
  	}
 }
collapse (mean) $leaderdv, by(village type1)
foreach v in $leaderdv {
	label var `v' "`l`v''"
 }
foreach var in $leaderdv{
preserve
collapse (mean) mean`var' = `var' (sd) sd`var' = `var' (count) n`var'=`var', by(type1)
label var mean`var' "`l`var''"
generate hi`var' = mean`var' + invttail(n`var'-1,0.05)*(sd`var'/sqrt(2*n`var'))
generate lo`var' = mean`var' - invttail(n`var'-1,0.05)*(sd`var'/sqrt(2*n`var'))
local vtext : variable label mean`var' 
twoway (bar mean`var' type1, barwidth(.75)) ///
(rcap hi`var' lo`var' type1), ///
graphregion(fcolor(white)) scheme(s2mono) name(`var') ///
xtitle("") ytitle(`"`vtext'"') ///
xlabel(1 "Diola" 2 "Toucouleur" 3 "Mouride") ///
legend( order(2 "5% Statistical Significance Bar") )
restore
}
graph display competitive
graph export "Graphs/competitive.pdf", replace
graph display candidates_p
graph export "Graphs/candidates_p.pdf", replace

preserve
collapse (mean) meanland = land (sd) sdland = land (count) nland=land (mean) meanland_hh = land_hh (sd) sdland_hh = land_hh (count) nland_hh=land_hh, by(type1)
label var meanland "`lland'"
label var meanland_hh "`lland_hh'"
generate hiland = meanland + invttail(nland-1,0.05)*(sdland/sqrt(2*nland))
generate loland = meanland - invttail(nland-1,0.05)*(sdland/sqrt(2*nland))
generate hiland_hh = meanland_hh + invttail(nland_hh-1,0.05)*(sdland_hh/sqrt(2*nland_hh))
generate loland_hh = meanland_hh - invttail(nland_hh-1,0.05)*(sdland_hh/sqrt(2*nland_hh))
twoway (bar meanland type1, barwidth(.75)) (rcap hiland loland type1) ///
(bar meanland_hh type1, barwidth(.75)) (rcap hiland_hh loland_hh type1), ///
graphregion(fcolor(white)) scheme(s2mono) name(autonomy) ///
xtitle("") ytitle("Landholding (in ha)") ///
xlabel(1 "Diola" 2 "Toucouleur" 3 "Mouride") ///
legend( order(1 "Leader mean" 2 "5% Statistical Significance" 3 "Participant mean" 4 "5% Statistical Significance") )
restore

graph combine autonomy abroad sourceswealth, graphregion(fcolor(white) ilcolor(white) lcolor(white)) scale(.7)
graph save "Graphs/AutonomyByType", replace
graph export "Graphs/AutonomyByType.pdf", replace

grc1leg transfer, legendfrom(transfer) graphregion(fcolor(white) ilcolor(white) lcolor(white)) 
graph save "Graphs/ReciprocityByType", replace
graph export "Graphs/ReciprocityByType.pdf", replace

graph drop _all
use "`main'", clear

* Group-level analysis of effect of anonymity treatment
foreach i in $types {
logit vote1 Anon namepolint_hh pnamepolint if type=="`i'", vce(cluster village)
estimates store `i'
}
estout Total $types, style(tex) starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(3)) se(par fmt(3))) varlabels(_cons Intercept) label collabels(none) stats(N,  fmt(%9.0g))

**Produce coefplot
coefplot Diola Toucouleur Mouride, ///
keep(Anonymitytreatment) xline(0) levels(95) xlabel(-4(1)4) coeflabel(,wrap(9)) ///
graphregion(fcolor(white) ilcolor(white) lcolor(white)) scheme(s1mono) ///
legend(col(4)) xtitle("Coefficient estimates with 95% confidence intervals")
graph export "Graphs/Anon.pdf", replace

* Broker-level analysis of impact of competitiveness/autonomy on vote choice

label variable Anon "Anonymity"
label variable namepolint_hh "Correctly named leader"
label variable nonag "Non-ag income"
label variable solid_hh "Pariticpant household material"
label var population "Village population"
label var transfer Reciprocity

logit vote1 compA autoA, vce(cluster village)
estimates store M1
logit vote1 c.compA c.autoA##Anon, vce(cluster village)
estimates store M2
logit vote1 c.compA c.autoA##Anon edlevel_hh solid_hh nonag population pnamepolint, vce(cluster village)
estimates store M3 

esttab M1 M2 M3  /// 
	using "Tables/main_new.tex", star(* 0.10 ** 0.05 *** 0.01) se b(3) se(3) r2(2) label alignment(D{.}{.}{-1}) booktabs replace ///
	stats(N,  fmt(%9.0g))  ///
	title(Institutional correlates of voting behavior \label{tab:main})  ///
	eqlabels(none) nonotes  nobaselevels nodepvars nogaps nomtitles ///
	sub("\begin{tabular}" "\small \begin{tabular}" "\begin{table}[htbp]" "\begin{table}[htbp] \setlength{\tabcolsep}{0pt} " "Anonymity=1" "Anonymity" "Reciprocity=1" "Reciprocity") ///
	addnote("Robust standard errors clustered at the village level. $^* p<0.10$, $^{**} p<0.05$, $^{***} p<0.01$")

* Individual-level analysis of impact of reciprocity on vote choice

logit vote1 i.noncash_hh namepolint_hh if replace_jeux!=1, vce(cluster village)
estimates store M1
logit vote1 i.noncash_hh##Anon namepolint_hh if replace_jeux!=1, vce(cluster village)
estimates store M2
logit vote1 c.autoA##i.noncash_hh namepolint_hh if replace_jeux!=1 & Anon==0, vce(cluster village)
estimates store M3
logit vote1 c.autoA##i.noncash_hh namepolint_hh compA edlevel_hh solid_hh nonag population if replace_jeux!=1 & Anon==0, vce(cluster village)
estimates store M4

esttab M1 M2 M3 M4 /// 
	using "Tables/main1.tex", star(* 0.10 ** 0.05 *** 0.01) se b(3) se(3) r2(2) label alignment(D{.}{.}{-1}) booktabs replace ///
	stats(N,  fmt(%9.0g))  ///
	title(Relationship between positive and negative inducements \label{tab:main1})  ///
	eqlabels(none) nonotes  nobaselevels nodepvars nogaps nomtitles ///
	sub("\begin{tabular}" "\small \begin{tabular}" "\begin{table}[htbp]" "\begin{table}[htbp] \setlength{\tabcolsep}{0pt} " "Anonymity=1" "Anonymity" "Yes" "Reciprocity") ///
	addnote("Robust standard errors clustered at the village level. $^* p<0.10$, $^{**} p<0.05$, $^{***} p<0.01$")

	
* Analysis of effect of deference to the leader

gen deference=(aut_part100-pol_int100)/aut_part100 if player==2
gen deference1=(aut_part100-pol_int100) if player==2

qui logit vote1 pol_int100 aut_part100 if village!=53 & village!=62 & type!="Tidjane" & player==2, vce(cluster village) 
estimates store Total
foreach i in $types {
qui logit vote1 pol_int100 aut_part100 if type=="`i'" & village!=53 & village!=62 & player==2, vce(cluster village)
estimates store `i'
}
estout Total $types, style(tex) starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(3)) se(par fmt(3))) varlabels(_cons Intercept) label collabels(none) stats(N,  fmt(%9.0g))


* Analysis of fairness norms

qui logit vote1 aut_part100 if village!=53 & village!=62 & type!="Tidjane" & player==1, vce(cluster village) 
estimates store Total
foreach i in $types {
qui logit vote1 aut_part100 if type=="`i'" & village!=53 & village!=62 & player==1, vce(cluster village)
estimates store `i'
}
estout Total $types, style(tex) starlevels(* 0.10 ** 0.05 *** 0.01) cells(b(star fmt(3)) se(par fmt(3))) varlabels(_cons Intercept) label collabels(none) stats(N,  fmt(%9.0g))

